<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\tiles;


class TilesRecords extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_tiles';

    const _CONFIG_SELECT_ = 'db_tiles';

    const _DATABASE_ = 'tiles';

    const _TABLE_NAME = 'tiles_records';

    const STATUS_NORMAL = 1;

    const RECORD_TYPE_TOPUP = 1;

    const RECORD_TYPE_COURCE = 2;

    const PAGE_SIZE = 20;

    const RECORD_TYPE_DESC = [
        self::RECORD_TYPE_TOPUP  => '充值',
        self::RECORD_TYPE_COURCE => '消费'
    ];

    public static function getRecordByTransactionId($transactionId)
    {
        return self::get("*", [
            'transaction_id' => $transactionId,
            'record_status'  => self::STATUS_NORMAL,
        ]);
    }

    public static function getRecordByOrderNo($orderNo)
    {
        return self::get("*", [
            'order_no'      => $orderNo,
            'record_status' => self::STATUS_NORMAL,
        ]);
    }

    public static function addTopupRecord($uid, $recordType, $amount, $orderNo, $transactionId, $record_info, $recordtime)
    {
        $result = self::insert([
            'uid'            => $uid,
            'record_type'    => $recordType,
            'amount'         => $amount,
            'order_no'       => $orderNo,
            'transaction_id' => $transactionId,
            'record_info'    => $record_info,
            'record_time'    => $recordtime,
            'addtime'        => date('Y-m-d H:i:s'),
            'record_status'  => self::STATUS_NORMAL
        ]);
        return $result->rowCount();
    }

    public static function getHistory($uid, $page, $per_page = self::PAGE_SIZE)
    {
        $count = self::count([
            'uid'           => $uid,
            'record_status' => self::STATUS_NORMAL
        ]);
        $history = self::select("*", [
            'uid'           => $uid,
            'record_status' => self::STATUS_NORMAL,
            'LIMIT'         => [
                ($page - 1) * $per_page,
                $per_page
            ],
            'ORDER'         => [
                'record_time' => 'DESC'
            ]
        ]);
        return [
            'is_voer'      => $count < $page * $per_page ? 1 : 0,
            'history_list' => $history
        ];
    }

    public static function admGetHistory($where)
    {
        $list = self::select('*', $where);
        unset($where['LIMIT']);
        $count = self::count($where);
        return [
            'list'  => $list,
            'total' => $count
        ];
    }

}